import React from 'react';
import {message} from 'antd';
import {ResetPasswordParam} from './data.d';
import {resetPassword} from './service';
import ProForm, { ProFormText } from '@ant-design/pro-form';

const handleResetPassword = async (values: ResetPasswordParam) => {
  const hide = message.loading('修改密码...')
  try {
    await resetPassword({...values}).then((resp) => {
      if (resp.status !== 200 ) {
        const errMsg = resp.data? resp.data : `errCode: ${resp.status}`;
        throw new Error(errMsg);
      }
    });
    hide();
    message.success('密码修改成功');
    return true;
  } catch (error) {
    hide();
    message.error(`密码修改失败, ${  error}`);
    return false;
  }

}
const UserSettings: React.FC = () => {
  return (
    <ProForm
      onFinish={async (values) => handleResetPassword({newPassword: values.newPassword})}
    >
      <ProFormText.Password
        name="newPassword"
        label="密码重置"
        placeholder="新密码"
        width="md"
      />
    </ProForm>
  )
};

export default UserSettings;
